package com.originskyseed.platform.user.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.BusinessRelateChannelMapper;
import com.originskyseed.platform.user.mapper.DeptMapper;
import com.originskyseed.platform.user.mapper.UserInfoMapper;
import com.originskyseed.platform.user.model.BusinessRelateChannel;
import com.originskyseed.platform.user.model.Dept;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IBusinessRelateChannelService;

/**
 * 业务员关联渠道信息表Service接口实现类（OSI_BUSINESS_RELATE_CHANNEL）
 *
 * @author WuZhuoXi
 */
@DubboService
@Component("businessRelateChannelService")
public class BusinessRelateChannelServiceImpl implements IBusinessRelateChannelService {

	@Resource
	private BusinessRelateChannelMapper businessRelateChannelMapper;

	@Resource
	private DeptMapper deptMapper;

	@Resource
	private UserInfoMapper userInfoMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.businessRelateChannelMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.businessRelateChannelMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateDeleteByIds(List<String> ids) throws Exception {
		return this.businessRelateChannelMapper.updateDeleteByIds(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(BusinessRelateChannel record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.businessRelateChannelMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(BusinessRelateChannel record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.businessRelateChannelMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(BusinessRelateChannel record) throws Exception {
		record.setUpdateTime(new Date());
		return this.businessRelateChannelMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(BusinessRelateChannel record) throws Exception {
		record.setUpdateTime(new Date());
		return this.businessRelateChannelMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public BusinessRelateChannel selectByPrimaryKey(String id) throws Exception {
		return this.businessRelateChannelMapper.selectByPrimaryKey(id);
	}

	/**
	 * 判断是否已经有绑定
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public BusinessRelateChannel selectIsChannelByBusinessId(String id) throws Exception {
		return this.businessRelateChannelMapper.selectIsChannelByBusinessId(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<BusinessRelateChannel> selectList(BusinessRelateChannel record) throws Exception {
		return this.businessRelateChannelMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<BusinessRelateChannel> selectListByPage(Map<String, Object> params, BasePage<BusinessRelateChannel> page, UserInfo userInfo) throws Exception {
		params.put("page", page);
		params.put("userIdList", this.userList(userInfo));
		List<BusinessRelateChannel> list = this.businessRelateChannelMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 导出
	 * 
	 * @param params
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<BusinessRelateChannel> selectListToExcel(Map<String, Object> params, UserInfo userInfo) throws Exception {
		params.put("userIdList", this.userList(userInfo));
		return this.businessRelateChannelMapper.selectListByPage(params);
	}

	/**
	 * 用户信息
	 * 
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	private List<String> userList(UserInfo userInfo) throws Exception {
		Dept dept = this.deptMapper.selectByPrimaryKey(userInfo.getDeptId());
		List<String> userIdList = new ArrayList<>();
		if (userInfo.getSourceSys() != 0) {
			if (dept != null) {
				List<String> deptIdList = new ArrayList<>();
				this.selectSonId(dept.getCode(), deptIdList);
				if (deptIdList != null && deptIdList.size() > 0) {
					userIdList = this.userInfoMapper.selectSalesmanUserIdsByDeptIds(removeDuplicate(deptIdList));
				}
			}

		}
		return userIdList;
	}

	/**
	 * 查询组织下的部门
	 * 
	 * @param code
	 * @param list
	 * @throws Exception
	 */
	private void selectSonId(String code, List<String> list) throws Exception {
		List<Dept> deptList = this.deptMapper.selectByParentCode(code);
		if (deptList.size() == 0) {
			Dept dept = this.deptMapper.selectByCode(code);
			list.add(dept.getId());
			return;
		} else {
			for (Dept dept1 : deptList) {
				list.add(dept1.getId());
				selectSonId(dept1.getCode(), list);
			}
		}
	}

	/**
	 * 删除重复项
	 * 
	 * @param list
	 * @return
	 */
	private List removeDuplicate(List list) {
		HashSet h = new HashSet(list);
		list.clear();
		list.addAll(h);
		return list;
	}

}
